/// <reference path="jquery.js"/>

(function($) {
	$.extend({
		gadget:{

			//*** internal *****************************************************
			
			internal:{
				blur    : null,
				focus   : null,
				sidebar : (typeof(System) != 'undefined'),
				visible : true,
				visibilityChanged: function(isVisible){
					if ($.gadget.sidebar()) {
						$.gadget.internal.visible = System.Gadget.visible;
					} else {
						$.gadget.internal.visible = isVisible;
					}
					if ($.gadget.internal.visible && $.gadget.internal.focus) {
						$.gadget.internal.focus();
					} else if (!$.gadget.internal.visible && $.gadget.internal.blur) {
						$.gadget.internal.blur();
					}
				}
			},
			
			
			//*** host *********************************************************
			
			debug    : function(){ return false; },
			browser  : function(){ return !$.gadget.internal.sidebar; },
			docked   : function(){ return ($.gadget.sidebar() ? System.Gadget.docked : ($.gadget.settings('docked') != 'false')); },
			undocked : function(){ return !$.gadget.docked(); },
			sidebar  : function(){ return $.gadget.internal.sidebar; },
			offline  : function(){ return ($.gadget.settings('offline') != 'false'); },
			visible  : function(){ return $.gadget.internal.visible; },
			
			
			//*** instance *****************************************************
			
			name    : function(){ return ($.gadget.sidebar() ? System.Gadget.name : 'Gadget'); },
			version : function(){ return ($.gadget.sidebar() ? System.Gadget.version : '0.0'); },


			//*** docking ******************************************************

			dock   : function(callback) { 
				if ($.gadget.sidebar()) {
					if (callback) { System.Gadget.onDock = callback; }
					else if (System.Gadget.onDock) { System.Gadget.onDock(); }
				}
				return $.gadget;
			},
			undock   : function(callback) { 
				if ($.gadget.sidebar()) {
					if (callback) { System.Gadget.onUndock = callback; }
					else if (System.Gadget.onUndock) { System.Gadget.onUndock(); }
				}
				return $.gadget;
			},


			//*** visibility ***************************************************

			focus: function(callback) { 
				var isWatched = (callback) ? true : ($.gadget.internal.blur);
				$.gadget.internal.focus = callback; 
				if ($.gadget.sidebar()){ 
					System.Gadget.visibilityChanged = (isWatched) ? $.gadget.internal.visibilityChanged : null;
				} else if (isWatched) {
					$(window).focus(function(){ $.gadget.internal.visibilityChanged(true); });
				} else {
					$(window).unbind('focus');
				}
				return $.gadget;
			},
			blur: function(callback) { 
				var isWatched = (callback) ? true : ($.gadget.internal.focus);
				$.gadget.internal.blur = callback; 
				if ($.gadget.sidebar()){ 
					System.Gadget.visibilityChanged = (isWatched) ? $.gadget.internal.visibilityChanged : null;
				} else if (isWatched) {
					$(window).blur(function(){ $.gadget.internal.visibilityChanged(false); });
				} else {
					$(window).unbind('blur');
				}
				return $.gadget;
			},
			

			//===| jQuery.gadget.flyout() |=====================================
			
			flyout: function(page, options){
				// if no page specified, close
				if (!page) {
					if ($.gadget.sidebar()) { System.Gadget.Flyout.show = false; }
					window.close();
					return $.gadget;
				}
				
				// append debug information, if in browser
				if ($.gadget.browser())
				{
					// show flyout/popup
					var hasQueryString = !$.string.isEmpty(document.location.search);
					var hasDebug = (options && !$.string.isEmpty(options.debug));
					var url = page + document.location.search
						+ (hasDebug ? (hasQueryString ? '&' : '?') : '')
						+ (hasDebug ? options.debug : '');
					var flyout = window.open(url);

					// set show/hide callback methods
					if (options && options.show) { $(flyout.document).load(options.show); }
					if (options && options.hide) { $(flyout.document).unload(options.hide); }

					// return gadget for piping
					return $.gadget;
				}
				
				// close it, if it's already open (prevents crashing the gadget)
				System.Gadget.Flyout.show = false;
				
				// open flyout
				System.Gadget.Flyout.onHide = (options && options.hide) ? options.hide : null;
				System.Gadget.Flyout.onShow = (options && options.show) ? options.show : null;
				System.Gadget.Flyout.file = page;
				System.Gadget.Flyout.show = true;
				
				// return gadget for piping
				return $.gadget;
			},


			//===| jQuery.gadget.settings() |===================================

			settings: function(p,v){
				// validate params
				if ($.undef(p)) { return; }
				
				// if there's a value, save and return
				if (arguments.length > 1){
					if ($.gadget.sidebar()) { System.Gadget.Settings.write(p,v); }
					return v;
				}
				
				// if browser, get from querystring
				if ($.gadget.browser())
				{
					var q = document.location.search;
					if (q == null || q.length == 0) { return; }
					q = q.substring(1).split("&");
					for (var i=0; i<q.length; i++){
						if (q[i].indexOf(p+'=') == 0){
							return q[i].substring((p + "=").length);
						}
					}
					return null;
				}
				
				// get from system
				return System.Gadget.Settings.read(p);
			}
		}
	});
})(jQuery);
